<?php


namespace app\shopConsole\service\freight;



use app\dao\shop\FreightDao;
use app\dao\shop\FreightSpecialDao;
use app\model\ShopFreight;
use think\facade\Db;

class FreightService
{
    /**
     * @var ShopFreight
     */
    private ShopFreight $shopFreight;

    public function __construct(ShopFreight $shopFreight)
    {
        $this->shopFreight = $shopFreight ;
    }

    public function create($data)
    {
        $freightDao = new FreightDao();

        Db::startTrans();
        try
        {
            $freight = $freightDao->create($data);

            $freightSpecialDao = new FreightSpecialDao();
            foreach ($data['special'] as $value)
            {
                $freightSpecialDao->create([
                    'freight_id' => $freight['id'],
                    'min_amount' => $value['min_amount'],
                    'per_amount' => $value['per_amount'],
                    'min_unit' => $value['min_unit'],
                    'per_unit' => $value['per_unit'],
                    'province' => $value['province'],
                    'city' => $value['city'],
                    'district' => $value['district'],
                ]);
            }
            Db::commit();
            return $freightDao->getById($freight['id']);
        }catch (\Exception $exception){
            Db::rollback();
            throw $exception;
        }
    }

    public function freightBan()
    {
        $freightDao = new FreightDao();
        $freightDao->update(['is_ban' => 1], $this->shopFreight->id);
        return $freightDao->getById($this->shopFreight->id);
    }

    public function freightUndo()
    {
        $freightDao = new FreightDao();
        $freightDao->update(['is_ban' => 0], $this->shopFreight->id);
        return $freightDao->getById($this->shopFreight->id);
    }

    public function update($data)
    {
        $freightDao = new FreightDao();

        Db::startTrans();
        try
        {
            $freight = $freightDao->updateById($data, $this->shopFreight->id);

            $freightSpecialDao = new FreightSpecialDao();
            foreach ($freight['special'] as $value)
            {
                $freightSpecialDao->deleteById($value['id']);
            }

            foreach ($data['special'] as $value)
            {
                $freightSpecialDao->create([
                    'freight_id' => $this->shopFreight->id,
                    'min_amount' => $value['min_amount'],
                    'per_amount' => $value['per_amount'],
                    'min_unit' => $value['min_unit'],
                    'per_unit' => $value['per_unit'],
                    'province' => $value['province'],
                    'city' => $value['city'],
                    'district' => $value['district'],
                ]);
            }
            Db::commit();
            return $freightDao->getById($this->shopFreight->id);
        }catch (\Exception $exception){
            Db::rollback();
            throw $exception;
        }
    }

    public function delete()
    {
        $freightDao = new FreightDao();
        $freightSpecialDao = new FreightSpecialDao();

        Db::startTrans();
        try
        {
            $freightDao->deleteByid($this->shopFreight->id);

            $specials = $freightSpecialDao->getByFreightId($this->shopFreight->id);
            foreach ($specials as $special){
                $freightSpecialDao->deleteByid($special['id']);
            }
            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            throw $exception;
        }
    }

}